package net.starwifi.duobao.service.impl;

import net.starwifi.duobao.jms.dto.SmsMessage;
import net.starwifi.duobao.service.JmsSendService;
import net.starwifi.duobao.service.SmsSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/1/29.
 */
@Service
public class SmsSendServiceImpl implements SmsSendService {


    @Autowired
    JmsSendService jmsSendService;

    @Override
    public void massSms(String templateId, String content,String contentTwo,String filePath) {
        List<String> fileContent=importCsv(filePath);
        for(String line:fileContent){
            if(line!=null&&!line.trim().equals("")){
                if(line.contains("\"")){
                    line=line.replace("\"","");
                }
                SmsMessage smsMessage=new SmsMessage(templateId,line,content,contentTwo,new Date());
                jmsSendService.sendTextMessage(smsMessage.toJson());
            }
        }
    }

    public static List<String> importCsv(String filePath){
        List<String> dataList=new ArrayList<String>();

        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader(new File(filePath)));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return dataList;
    }


    public List<List<String>> readCSVFile(String filePath) {
        InputStreamReader fr=null;
        BufferedReader br=null;
        List<List<String>> listFile=null;
        try {
             fr=new InputStreamReader(new FileInputStream(filePath));
             br = new BufferedReader(fr);
            String rec = null;// 一行
            String str;// 一个单元格
            listFile = new ArrayList<List<String>>();
            while ((rec = br.readLine()) != null) {
                Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
                Matcher mCells = pCells.matcher(rec);
                List<String> cells = new ArrayList<String>();// 每行记录一个list
                // 读取每个单元格
                while (mCells.find()) {
                    str = mCells.group();
                    str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
                    str = str.replaceAll("(?sm)(\"(\"))", "$2");
                    cells.add(str);
                }
                listFile.add(cells);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
                if (br != null) {
                    br.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return listFile;
    }
}
